package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.sun.corba.se.spi.orbutil.fsm.State;
import com.ysd.entity.Banner;
import com.ysd.util.DBManager;

public class BannerDaoImpl {
	/**
	 * 显示所有轮播图
	 * @return
	 */
	public List<Banner> getBanner() {
		List<Banner> list=new ArrayList<Banner>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM banner");
			while(rs.next()) {
				int bannerId=rs.getInt("bannerId");
				String bannerimg=rs.getString("bannerimg");
				Banner b=new Banner(bannerId,bannerimg);
				list.add(b);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn,st,rs);
		}
		
	}
	/**
	 * 分页查询
	 * @return
	 */
	public List<Banner> getBannerByPage(int page,int size) {
		List<Banner> list=new ArrayList<Banner>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			String sql = "select * from banner limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int bannerId=rs.getInt("bannerId");
				String bannerimg=rs.getString("bannerimg");
				Banner b=new Banner(bannerId,bannerimg);
				list.add(b);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn,st,rs);
		}
		
	}
	/**
	 * 删除轮播图
	 * @param bId
	 * @return
	 */
	public int delBanner(int bId) {
		return DBManager.executeUpdate("DELETE FROM banner WHERE bannerId="+bId);
	}
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	public int deleteBannerByIds(String ids) { // 
		String sql = "delete from banner where bannerId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	/**
	 * 添加轮播图
	 * @param t
	 * @return
	 */
	public int addBanner(Banner t) {
		return DBManager.executeUpdate("INSERT INTO banner VALUES(null,'"+t.getBannerimg()+"')");
	}
	/**
	 * 数据回显
	 * @param bId
	 * @return
	 */
	public Banner getBannerId(int bId) {
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		Banner b=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM banner WHERE bannerId="+bId);
			while(rs.next()) {
				int bid=rs.getInt("bannerId");
				String img=rs.getString("bannerimg");
				b=new Banner(bid, img);
			}
			return b;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	/**
	 * 修改
	 * @param d
	 * @return
	 */
	public int updateBanner(Banner d) {
		return DBManager.executeUpdate("UPDATE banner set bannerimg='"+d.getBannerimg()+"' WHERE bannerId="+d.getBannerId());
	}
}
